var stepId = $CertusJsonAjax.getStepInstanceId();
var processId = $CertusJsonAjax.getProInstanceId();
var isHasMg = "0";
$CertusFormAjax.ajax("findLxInfo",{"processId":processId}, function(result){
    var data = result.data.map;
    $$scope.proNo = data.pro_no;
    $$scope.proArea = data.pro_area;
    var proType = data.pro_type;
    $$scope.typeCode=proType;
    if(proType=="ZL"){
        $$scope.proType="战略类";
    }else if (proType=="YF"){
        $$scope.proType="研发类";
    }else if (proType=="XS"){
        $$scope.proType="销售类";
        $$scope.isXS=true;
    }else if (proType=="BM"){
        $$scope.proType="部门类";
    }else if (proType=="SW"){
        $$scope.proType="事务类";
    }else if (proType=="CS"){
        $$scope.proType="测试类";
    }
    $$scope.proName = data.pro_name;
    $$scope.lxLevel = data.pro_lv;
    if (data.pro_lv=="3"){
        $$scope.lxLevel = "高";
    }else if (data.pro_lv=="2"){
        $$scope.lxLevel = "中";
    }else{
        $$scope.lxLevel = "低";
    }
    $("#lxLevel").val(data.pro_lv);
    //如果是 自己勾选的项目经理，则不能更改
    if (data.is_mg=="true"){
        $("#proMgName").attr("disabled",true);
    };

    if (proType=="XS"){
        $$scope.showSign = true;
        $$scope.showHk = true;
        $$scope.showArea = true;
        $$scope.showCus = true;
        $$scope.isCompare=true;
        $$scope.proArea = data.pro_area;

        var cusName = data.cus_name.split("-----");
        $$scope.cusName = cusName[0];
        if(data.is_compare=="0"){
            $$scope.isCompare = "无";
        }else{
            $$scope.isCompare = "有";
        }
        $$scope.divineSignDate = data.divine_sign_date;
        $$scope.divineHkDate = data.divine_hk_date;
    }else if (proType=="ZL"||proType=="SW"
        ||proType=="BM"){
        $$scope.showFinish = true;
        $$scope.isCompare=false;
        $$scope.divineFinishTime = data.divine_finish_time;
    }else {
        $$scope.showVersion = true;
        $$scope.isCompare=false;
        $$scope.divineVersionTime = data.divine_version_time
    }

    if (data.urgency==1){
        $$scope.urgency="紧急";
    }else{
        $$scope.urgency="不紧急";
    }

    if (data.importance==0){
        $$scope.importance="一般";
    }else if (data.importance==1){
        $$scope.importance="重要";
    }else{
        $$scope.importance="战略";
    }
    $$scope.clueDesc = data.clue_desc;
    $$scope.cusPolicy = data.cus_policy;
    $$scope.competitor = data.competitor;
    $$scope.lxDesc = data.lx_desc;
    $$scope.proTarget = data.pro_target;
    $$scope.result = data.result;
    $$scope.divineMoney = data.divine_money;
    $$scope.cusBudget = data.cus_budget;
    $$scope.startTime = data.start_time;
    $$scope.endTime = data.end_time;
    $$scope.opinionMsg = data.opinion_msg;
    $$scope.proLv = data.pro_lv+"";
    if (data.selName!=null&&data.selName!=undefined){
        $$scope.selName = data.selName+"("+data.selId+")"+"["+data.bsPositonName+"]";
        $$scope.proSelId = data.selId;
    }

    // if (data.selName!=null&&data.selName!=""){
    //     $("#autocompleteSel").attr('disabled',true);
    //     $("#autocompleteSel").parent().append('' +
    //         '<a href="javascript:void(0);" class="auto-reset" onclick="reset()">重选</a>');
    // }


    $$scope.proId = data.pro_id;
    $$scope.lxId = data.id;
    $$scope.deptNames = result.data.deptNames;

    var mgId = data.pro_mg_id;
    if (mgId==null||mgId==""||mgId==undefined){
        $("#isHide").hide();
        //是否已指定项目经理
        isHasMgId();
    }else{
        //指定项目经理
        $$scope.proMgId=mgId;
        $$scope.result = data.result;
        $$scope.userName = data.user_name+"("+mgId+")"+"["+data.positon_name+"]";
        $$scope.divineContactMoney = data.divine_contact_money;
        $$scope.divineBusinessMoney = data.divine_business_money;
        $$scope.divinePurchaseMoney = data.divine_purchase_money;
        isHasMgId();
        $$scope.mgTs = "（由"+data.mgName+"担任项目经理）";
        isHasMg="1";
        $$scope.isMgId = true;
    }

    var arr = result.data.bugetList;
    var other1Columns = [{
        id: 'bugetPhase',
        title: '阶段',
        type: 'string',
        width: 100,
        columnClass: 'text-center',
    },{
        id: 'bugetType',
        title: '预算类型',
        type: 'string',
        width: 100,
        columnClass: 'text-center',
    }, {
        id: 'buget',
        title: '预算金额',
        type: 'string',
        width: 150,
        columnClass: 'text-center',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            var content = '￥' + value + '元';
            return content;
        }
    },{
        id: 'budgetBegin',
        title: '开始时间',
        type: 'string',
        width: 100,
        columnClass: 'text-center',
        resolution: function (value) {
            var content = '';
            if (value==""){
                return content;
            }else{
                var date = new Date(value);
                content = $.fn.dlshouwen.grid.tools.dateFormat(date, "yyyy-MM-dd");
                return content;
            }

        }
    },{
        id: 'budgetEnd',
        title: '结束时间',
        type: 'string',
        width: 100,
        columnClass: 'text-center',
        resolution: function (value) {
            var content = '';
            if (value==""){
                return content;
            }else{
                var date = new Date(value);
                content = $.fn.dlshouwen.grid.tools.dateFormat(date, "yyyy-MM-dd");
                return content;
            }

        }
    },{
        id: 'comments',
        title: '费用说明',
        type: 'string',
        columnClass: 'text-left'
    }];
    var other1Option = {
        lang: 'zh-cn',
        ajaxLoad: false,
        extraWidth: 20,
        // loadURL:'url',
        exportFileName: '商务预测费用',
        datas: arr,
        columns: other1Columns,
        gridContainer: 'other1Container',
        tools: ''
    };
    var other1Grid = $.fn.dlshouwen.grid.init(other1Option);
    other1Grid.load();


    var proinColumns = [{
        id: 'wntyName',
        title: '指标名称',
        width: 150,
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'wntyDesc',
        title: '指标描述',
        type: 'string',
        columnClass: 'text-left',
    }, {
        id: 'finishTime',
        title: '完成时间',
        type: 'string',
        width: 100,
        columnClass: 'text-center',
    }];
    var warrList = result.data.warrList;
    var proinOption = {
        lang: 'zh-cn',
        ajaxLoad: false,
        extraWidth: 20,
        // loadURL:'url',
        exportFileName: '项目指标',
        datas:warrList ,
        columns: proinColumns,
        gridContainer: 'proinContainer',
        tools: ''
    };
    var proinGrid = $.fn.dlshouwen.grid.init(proinOption);
    proinGrid.load();

    var prointColumns = [{
        id: 'proTarget',
        title: '项目目标',
        type: 'string',
        columnClass: 'text-left',
    },{
        id: 'finishTime',
        title: '完成时间',
        type: 'date',
        width: 100,
        format:'yyyy-MM-dd',
        otype:'time_stamp_ms',
        columnClass: 'text-center',
    }];
    var targetList = result.data.targetList;
    var prointOption = {
        lang: 'zh-cn',
        ajaxLoad: false,
        extraWidth: 20,
        // loadURL:'url',
        exportFileName: '项目目标',
        datas:targetList ,
        columns: prointColumns,
        gridContainer: 'prointContainer',
        tools: ''
    };
    var prointGrid = $.fn.dlshouwen.grid.init(prointOption);
    prointGrid.load();

    //人天预估
    var rlColumns = [{
        id: 'roleName',
        title: '角色',
        type: 'string',
        columnClass: 'text-center'
    },{
        id: 'level',
        title: '级别',
        type: 'string',
        columnClass: 'text-center',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            var content = '';
            if (value=="1"){
                content = "低";
            }else if (value=="2"){
                content = "中";
            }else{
                content = "高";
            }
            return content;
        }
    },{
        id: 'beginTime',
        title: '开始时间',
        type: 'date',
        format: 'yyyy-MM-dd',
        otype: 'time_stamp_ms',
        columnClass: 'text-center',
    }, {
        id: 'endTime',
        title: '结束时间',
        type: 'date',
        format: 'yyyy-MM-dd',
        otype: 'time_stamp_ms',
        columnClass: 'text-center',
    },{
        id: 'rtNum',
        title: '天数',
        type: 'string',
        columnClass: 'text-center',
    },{
        id: 'pNum',
        title: '人数',
        type: 'string',
        columnClass: 'text-center'
    }, {
        id: 'comments',
        title: '说明',
        type: 'string',
        columnClass: 'text-center',
    }];
    var rlOption = {
        lang: 'zh-cn',
        ajaxLoad: false,
        extraWidth: 20,
        // loadURL:'url',
        exportFileName: '人力成本列表',
        datas: result.data.rtList,
        columns: rlColumns,
        gridContainer: 'rtContainer',
    };
    var rlGrid = $.fn.dlshouwen.grid.init(rlOption);
    rlGrid.load();

    //涉及文件
    var wjColumns = [{
        id: 'productName',
        title: '文件名称',
        type: 'string',
        columnClass: 'text-center'
    }, {
        id: 'comments',
        title: '说明',
        type: 'string',
        columnClass: 'text-center',
    }];
    var wjOption = {
        lang: 'zh-cn',
        ajaxLoad: false,
        extraWidth: 20,
        // loadURL:'url',
        exportFileName: '人力成本列表',
        datas: result.data.wjList,
        columns: wjColumns,
        gridContainer: 'wjContainer'
    };
    var wjGrid = $.fn.dlshouwen.grid.init(wjOption);
    wjGrid.load();

    //所属产品
    var isNlx = false;
    //判断是否是新立项流程
    var pduList = result.data.pduList;
    for (var i=0;i<pduList.length;i++){
        //如果有记录为1，则为新立项流程
        if (pduList[i].setIsNLx = 1){
            isNlx = true;
        }
    }
    if (isNlx){
        var arr = pduList[0];
        $$scope.products = arr.productName;
    }else{
        //加载多个产品信息
    }

    //特殊立项流程
    $$scope.pronMgUser="";
    $$scope.pronXsUser="";
    $$scope.pronCpUser="";
    $$scope.pronJfUser="";

    var memberList = result.data.mebMap;
    for (var i = 0; i < memberList.length; i++) {
        var codes = memberList[i].proRoleCode.split(",");
        for (var j=0;j<codes.length;j++){
            //indexOf 大小写敏感
            if (codes[j].indexOf("XMJL")>-1) {
                $$scope.pronMgUser += memberList[i].userName+" ";
            }
            if (codes[j].indexOf("CPJL")>-1) {
                $$scope.pronXsUser += memberList[i].userName+" ";
            }
            if (codes[j].indexOf("JFJL")>-1) {
                $$scope.pronCpUser += memberList[i].userName+" ";
            }
            if (codes[j].indexOf("XSJL")>-1) {
                $$scope.pronJfUser += memberList[i].userName+" ";
            }
        }
    }

});

// function reset() {
//     $("#autocompleteSel").val('');
//     $("#proSelId").val('');
//     $("#autocompleteSel").attr('disabled',false);
//     $(".auto-reset").remove();
// }


function isHasMgId() {
    $CertusFormAjax.ajax("findSpUserList",{},function (response) {
        var nhlTeams = [];
        var data = response.data;
        for (var i=0;i<data.length;i++){
            var arr={};
            arr.name=data[i].userName+"["+data[i].positonName+"]";
            arr.id = data[i].userId;
            nhlTeams.push(arr);
        }
        //自动补全
        var nhl = $.map(nhlTeams, function (obj) {
            return {
                value: obj.name,
                data: { category: obj.id }
            };
        });
        //产品经理
        $('#autocompleteSel').devbridgeAutocomplete({
            lookup: nhl,
            minChars: 1,
            onSelect: function (suggestion) {
                $("#proSelId").val(suggestion.data.category);
            },
            showNoSuggestionNotice: true,
            noSuggestionNotice: 'Sorry, no matching results'
        });
    });
};
//项目其他费用表单显示隐藏
function hideother() {
    $(".hideother").hide();
};

function showother() {
    $(".hideother").show();
};
//申请通过
$(".submit").on('click',function () {
    if ($$scope.isXS==true){
        var isHasSel = $("#proSelId").val();
        if (isHasSel==""){
            layer.msg("请选择销售经理!");
            return;
        }
    }

    var lxLevel = $("#lxLevel").val();
    if (lxLevel=="0"){
        layer.msg("请选择项目优先级!");
        return;
    }
    // if (!certusValidateNotnull()){
    //     return;
    // }
    var params ={};
    params.proSelId = $("#proSelId").val();
    params.processId = processId;
    params.proMgName = $("#proMgName").val();
    params.stepId = stepId;
    params.lxLevel = lxLevel;
    params.proId = $("#proId").val();
    params.lxId = $("#lxId").val();
    params.isHasMg = isHasMg;
    params.optionMsg = $("#optionMsg").val();
    params.proType= $$scope.typeCode;
    // 开启
    var index = layer.load(1);
    $CertusFormAjax.ajax('toSelMgSp',{'params':JSON.stringify(params)},function () {
        //关闭
       layer.close(index);
       layer.msg("审批成功");
       window.history.back();
    });
});
//申请不通过
$(".unSubmit").on('click',function () {
    var params ={};
    params.optionMsg = $("#optionMsg").val();
    params.processId = processId;
    params.stepId = stepId;
    params.lxLevel = $("#lxLevel").val();
    params.proId = $("#proId").val();
    params.lxId = $("#lxId").val();
    // 开启
    var index = layer.load(1);
    $CertusFormAjax.ajax('toSpLxInfo2',{'params':JSON.stringify(params)},function () {
        //关闭
        layer.close(index);
        layer.msg("审批成功");
        window.history.back()
    });
});
//申请退回
$(".rSubmit").on('click',function () {
    var params ={};
    params.optionMsg = $("#optionMsg").val();
    params.processId = processId;
    params.stepId = stepId;
    params.lxLevel = $("#lxLevel").val();
    params.proId = $("#proId").val();
    params.lxId = $("#lxId").val();
    // 开启
    var index = layer.load(1);
    $CertusFormAjax.ajax('rhLxSq',{'params':JSON.stringify(params)},function () {
        //关闭
        layer.close(index);
        layer.msg("审批成功");
        window.history.back()
    });
})